﻿Imports Ocean.OceanFramework.MVVM
Imports Ocean.OceanFramework.CommonDialog

Namespace MVVM

    Public NotInheritable Class ViewModelUIService

#Region " Declarations "

        Private Shared _enumRunState As RunState = MVVM.RunState.Application
        Private Shared _objUnitTestResponder As IViewModelUIServiceUnitTestResponder

#End Region

#Region " Events "

        Public Shared Event Log(ByVal sender As Object, ByVal e As LogEventArgs)

#End Region

#Region " Properties "

        Public Shared ReadOnly Property RunState() As RunState
            Get
                Return _enumRunState
            End Get
        End Property

#End Region

#Region " Constructors "

        Private Sub New()
        End Sub

#End Region

#Region " Unit Test Methods "

        Public Shared Sub SetViewModelUIServiceForUnitTesting(ByVal objUnitTestResponder As IViewModelUIServiceUnitTestResponder)
            _enumRunState = MVVM.RunState.UnitTest
            _objUnitTestResponder = objUnitTestResponder
        End Sub

#End Region

#Region " Log Exception "

        Public Shared Sub LogException(ByVal ex As Exception)
            OnLog(ex)
        End Sub

#End Region

#Region " Log Messages "

        Public Shared Sub LogMessage(ByVal strTitle As String, ByVal strMessage As String)
            OnLog(CustomDialogResult.None, strTitle, strMessage, 0, Nothing)
        End Sub

#End Region

#Region " Show Custom Dialog Methods "

        Public Shared Function AllOptionsDialog(ByVal strCaption As String, ByVal strInstructionHeading As String, ByVal strInstructionText As String, ByVal enumInstructionIcon As CustomDialogIcon, ByVal strFooterText As String, ByVal enumFooterIcon As CustomDialogIcon, ByVal strAdditionalDetailsText As String, ByVal enumButtons As CustomDialogButton, ByVal enumDefaultButton As CustomDialogResult, Optional ByVal intButtonsDisabledDelay As Integer = 0) As CustomDialogResult
            Return ShowCustomDialog(strCaption, strInstructionHeading, strInstructionText, enumInstructionIcon, strFooterText, enumFooterIcon, strAdditionalDetailsText, enumButtons, enumDefaultButton, intButtonsDisabledDelay, Nothing)
        End Function

        Public Shared Sub ExceptionDialog(ByVal ex As Exception, Optional ByVal intButtonsDisabledDelay As Integer = 0)

#If DEBUG Then
            ShowCustomDialog("Exception", "Exception", ex.Message, CustomDialogIcon.Warning, String.Empty, CustomDialogIcon.None, ex.StackTrace, CustomDialogButton.OK, CustomDialogResult.OK, intButtonsDisabledDelay, ex)

#Else
            ShowCustomDialog("Exception", "Exception", ex.Message, CustomDialogIcon.Warning, String.Empty, CustomDialogIcon.None, String.Empty, CustomDialogButton.OK, CustomDialogResult.OK, intButtonsDisabledDelay, ex)
#End If

        End Sub

        Public Shared Sub ExceptionDialog(ByVal ex As Exception, ByVal strCaption As String, ByVal strInstructionHeading As String, Optional ByVal intButtonsDisabledDelay As Integer = 0)

#If DEBUG Then
            ShowCustomDialog(strCaption, strInstructionHeading, ex.Message, CustomDialogIcon.Warning, String.Empty, CustomDialogIcon.None, ex.StackTrace, CustomDialogButton.OK, CustomDialogResult.OK, intButtonsDisabledDelay, ex)

#Else
            ShowCustomDialog(strCaption, strInstructionHeading, ex.Message, CustomDialogIcon.Warning, String.Empty, CustomDialogIcon.None, String.Empty, CustomDialogButton.OK, CustomDialogResult.OK, intButtonsDisabledDelay, ex)
#End If

        End Sub

        Public Shared Sub ExceptionDialog(ByVal ex As Exception, ByVal strCaption As String, ByVal strInstructionHeading As String, ByVal strInstructionText As String, Optional ByVal intButtonsDisabledDelay As Integer = 0)

#If DEBUG Then
            ShowCustomDialog(strCaption, strInstructionHeading, strInstructionText, CustomDialogIcon.Warning, String.Empty, CustomDialogIcon.None, ex.StackTrace, CustomDialogButton.OK, CustomDialogResult.OK, intButtonsDisabledDelay, ex)

#Else
            ShowCustomDialog(strCaption, strInstructionHeading, strInstructionText, CustomDialogIcon.Warning, String.Empty, CustomDialogIcon.None, String.Empty, CustomDialogButton.OK, CustomDialogResult.OK, intButtonsDisabledDelay, ex)
#End If

        End Sub

        Public Shared Sub ExceptionDialog(ByVal ex As Exception, ByVal strCaption As String, ByVal strInstructionHeading As String, ByVal strInstructionText As String, ByVal enumInstructionIcon As CustomDialogIcon, ByVal strFooterText As String, ByVal enumFooterIcon As CustomDialogIcon, ByVal strAdditionalDetailsText As String, ByVal enumButtons As CustomDialogButton, ByVal enumDefaultButton As CustomDialogResult, Optional ByVal intButtonsDisabledDelay As Integer = 0)
            ShowCustomDialog(strCaption, strInstructionHeading, strInstructionText, enumInstructionIcon, strFooterText, enumFooterIcon, strAdditionalDetailsText, enumButtons, enumDefaultButton, intButtonsDisabledDelay, ex)
        End Sub

        Public Shared Sub MessageDialog(ByVal strCaption As String, ByVal strInstructionHeading As String, ByVal strInstructionText As String, Optional ByVal intButtonsDisabledDelay As Integer = 0)
            ShowCustomDialog(strCaption, strInstructionHeading, strInstructionText, CustomDialogIcon.Information, String.Empty, CustomDialogIcon.None, String.Empty, CustomDialogButton.OK, CustomDialogResult.OK, intButtonsDisabledDelay, Nothing)
        End Sub

        Public Shared Sub MessageDialog(ByVal strCaption As String, ByVal strInstructionHeading As String, ByVal strInstructionText As String, ByVal strFooterText As String, Optional ByVal intButtonsDisabledDelay As Integer = 0)
            ShowCustomDialog(strCaption, strInstructionHeading, strInstructionText, CustomDialogIcon.Information, strFooterText, CustomDialogIcon.Information, String.Empty, CustomDialogButton.OK, CustomDialogResult.OK, intButtonsDisabledDelay, Nothing)
        End Sub

        Public Shared Sub MessageDialog(ByVal strCaption As String, ByVal strInstructionHeading As String, ByVal strInstructionText As String, ByVal strFooterText As String, ByVal strAdditionalDetailsText As String, Optional ByVal intButtonsDisabledDelay As Integer = 0)
            ShowCustomDialog(strCaption, strInstructionHeading, strInstructionText, CustomDialogIcon.Information, strFooterText, CustomDialogIcon.Information, strAdditionalDetailsText, CustomDialogButton.OK, CustomDialogResult.OK, intButtonsDisabledDelay, Nothing)
        End Sub

        Public Shared Function YesNoCancelConfirmSave(Optional ByVal intButtonsDisabledDelay As Integer = 0) As CustomDialogResult
            Return ShowCustomDialog("Save", "Confirm Save", "Would you like to save your data before closing this form?", CustomDialogIcon.Question, String.Empty, CustomDialogIcon.None, String.Empty, CustomDialogButton.YesNoCancel, CustomDialogResult.No, intButtonsDisabledDelay, Nothing)
        End Function

        Public Shared Function YesNoCancelConfirmSave(ByVal strFooterText As String, Optional ByVal intButtonsDisabledDelay As Integer = 0) As CustomDialogResult
            Return ShowCustomDialog("Save", "Confirm Save", "Would you like to save your data before closing this form?", CustomDialogIcon.Question, strFooterText, CustomDialogIcon.Information, String.Empty, CustomDialogButton.YesNoCancel, CustomDialogResult.No, intButtonsDisabledDelay, Nothing)
        End Function

        Public Shared Function YesNoCancelConfirmSave(ByVal strFooterText As String, ByVal strAdditionalDetailsText As String, Optional ByVal intButtonsDisabledDelay As Integer = 0) As CustomDialogResult
            Return ShowCustomDialog("Save", "Confirm Save", "Would you like to save your data before closing this form?", CustomDialogIcon.Question, strFooterText, CustomDialogIcon.Information, strAdditionalDetailsText, CustomDialogButton.YesNoCancel, CustomDialogResult.No, intButtonsDisabledDelay, Nothing)
        End Function

        Public Shared Function YesNoCancelDialog(ByVal strCaption As String, ByVal strInstructionHeading As String, ByVal strInstructionText As String, Optional ByVal intButtonsDisabledDelay As Integer = 0) As CustomDialogResult
            Return ShowCustomDialog(strCaption, strInstructionHeading, strInstructionText, CustomDialogIcon.Question, String.Empty, CustomDialogIcon.None, String.Empty, CustomDialogButton.YesNoCancel, CustomDialogResult.Yes, intButtonsDisabledDelay, Nothing)
        End Function

        Public Shared Function YesNoCancelDialog(ByVal strCaption As String, ByVal strInstructionHeading As String, ByVal strInstructionText As String, ByVal strFooterText As String, Optional ByVal intButtonsDisabledDelay As Integer = 0) As CustomDialogResult
            Return ShowCustomDialog(strCaption, strInstructionHeading, strInstructionText, CustomDialogIcon.Question, strFooterText, CustomDialogIcon.Information, String.Empty, CustomDialogButton.YesNoCancel, CustomDialogResult.Yes, intButtonsDisabledDelay, Nothing)
        End Function

        Public Shared Function YesNoCancelDialog(ByVal strCaption As String, ByVal strInstructionHeading As String, ByVal strInstructionText As String, ByVal strFooterText As String, ByVal strAdditionalDetailsText As String, Optional ByVal intButtonsDisabledDelay As Integer = 0) As CustomDialogResult
            Return ShowCustomDialog(strCaption, strInstructionHeading, strInstructionText, CustomDialogIcon.Question, strFooterText, CustomDialogIcon.Information, strAdditionalDetailsText, CustomDialogButton.YesNoCancel, CustomDialogResult.Yes, intButtonsDisabledDelay, Nothing)
        End Function

        Public Shared Function YesNoConfirmDelete(Optional ByVal intButtonsDisabledDelay As Integer = 0) As CustomDialogResult
            Return ShowCustomDialog("Delete", "Confirm Delete", "Are you sure you want to delete this data?", CustomDialogIcon.Question, String.Empty, CustomDialogIcon.None, String.Empty, CustomDialogButton.YesNo, CustomDialogResult.No, intButtonsDisabledDelay, Nothing)
        End Function

        Public Shared Function YesNoConfirmDelete(ByVal strFooterText As String, Optional ByVal intButtonsDisabledDelay As Integer = 0) As CustomDialogResult
            Return ShowCustomDialog("Delete", "Confirm Delete", "Are you sure you want to delete this data?", CustomDialogIcon.Question, strFooterText, CustomDialogIcon.Information, String.Empty, CustomDialogButton.YesNo, CustomDialogResult.No, intButtonsDisabledDelay, Nothing)
        End Function

        Public Shared Function YesNoConfirmDelete(ByVal strFooterText As String, ByVal strAdditionalDetailsText As String, Optional ByVal intButtonsDisabledDelay As Integer = 0) As CustomDialogResult
            Return ShowCustomDialog("Delete", "Confirm Delete", "Are you sure you want to delete this data?", CustomDialogIcon.Question, strFooterText, CustomDialogIcon.Information, strAdditionalDetailsText, CustomDialogButton.YesNo, CustomDialogResult.No, intButtonsDisabledDelay, Nothing)
        End Function

        Public Shared Function YesNoDialog(ByVal strCaption As String, ByVal strInstructionHeading As String, ByVal strInstructionText As String, Optional ByVal intButtonsDisabledDelay As Integer = 0) As CustomDialogResult
            Return ShowCustomDialog(strCaption, strInstructionHeading, strInstructionText, CustomDialogIcon.Question, String.Empty, CustomDialogIcon.None, String.Empty, CustomDialogButton.YesNo, CustomDialogResult.Yes, intButtonsDisabledDelay, Nothing)
        End Function

        Public Shared Function YesNoDialog(ByVal strCaption As String, ByVal strInstructionHeading As String, ByVal strInstructionText As String, ByVal strFooterText As String, Optional ByVal intButtonsDisabledDelay As Integer = 0) As CustomDialogResult
            Return ShowCustomDialog(strCaption, strInstructionHeading, strInstructionText, CustomDialogIcon.Question, strFooterText, CustomDialogIcon.Information, String.Empty, CustomDialogButton.YesNo, CustomDialogResult.Yes, intButtonsDisabledDelay, Nothing)
        End Function

        Public Shared Function YesNoDialog(ByVal strCaption As String, ByVal strInstructionHeading As String, ByVal strInstructionText As String, ByVal strFooterText As String, ByVal strAdditionalDetailsText As String, Optional ByVal intButtonsDisabledDelay As Integer = 0) As CustomDialogResult
            Return ShowCustomDialog(strCaption, strInstructionHeading, strInstructionText, CustomDialogIcon.Question, strFooterText, CustomDialogIcon.Information, strAdditionalDetailsText, CustomDialogButton.YesNo, CustomDialogResult.Yes, intButtonsDisabledDelay, Nothing)
        End Function

#End Region

#Region " Show Folder Browser Methods "

        Public Shared Function ShowFolderBrowser(ByVal strDescription As String, ByVal bolShowNewFolderButton As Boolean) As String

            Dim strResponse As String = String.Empty

            If _enumRunState = MVVM.RunState.Application Then

                Dim obj As New CommonDialog.FolderBrowserDialog
                With obj
                    .Description = strDescription
                    .ShowNewFolderButton = bolShowNewFolderButton
                End With

                If obj.ShowDialog Then
                    strResponse = obj.SelectedPath
                End If

            Else
                strResponse = _objUnitTestResponder.GetFolder
            End If

            Return strResponse
        End Function

#End Region

#Region " Methods "

        Private Shared Sub OnLog(ByVal enumCustomDialogResult As CustomDialogResult, ByVal strInstructionHeading As String, ByVal strInstructionText As String, ByVal intButtonsDisabledDelay As Integer, ByVal ex As Exception)

            If LogEvent IsNot Nothing Then
                RaiseEvent Log(Nothing, New LogEventArgs(enumCustomDialogResult, strInstructionHeading, strInstructionText, intButtonsDisabledDelay, ex))
            End If

        End Sub

        Private Shared Sub OnLog(ByVal ex As Exception)

            If LogEvent IsNot Nothing Then
                RaiseEvent Log(Nothing, New LogEventArgs(ex))
            End If

        End Sub

        Private Shared Function ShowCustomDialog(ByVal strCaption As String, ByVal strInstructionHeading As String, ByVal strInstructionText As String, ByVal enumInstructionIcon As CustomDialogIcon, ByVal strFooterText As String, ByVal enumFooterIcon As CustomDialogIcon, ByVal strAdditionalDetailsText As String, ByVal enumButtons As CustomDialogButton, ByVal enumDefaultButton As CustomDialogResult, ByVal intButtonsDisabledDelay As Integer, ByVal ex As Exception) As CustomDialogResult

            'TODO developers - use this StackTrace information if your application requires this.
            'get the calling code information
            Dim objTrace As System.Diagnostics.StackTrace = New System.Diagnostics.StackTrace()
            Dim strCallingReflectedTypeName = objTrace.GetFrame(1).GetMethod.ReflectedType.Name
            Dim strCallingMethodName = objTrace.GetFrame(1).GetMethod.Name
            Dim strCallingModuleName = objTrace.GetFrame(1).GetMethod.Module.Name
            Dim enumResult As CustomDialogResult = CustomDialogResult.None

            If _enumRunState = MVVM.RunState.Application Then

                Dim obj As New CommonDialog.CustomDialog
                With obj
                    .AdditionalDetailsText = strAdditionalDetailsText
                    .Buttons = enumButtons
                    .ButtonsDisabledDelay = intButtonsDisabledDelay
                    .Caption = strCaption
                    .DefaultButton = enumDefaultButton
                    .FooterIcon = enumFooterIcon
                    .FooterText = strFooterText
                    .InstructionHeading = strInstructionHeading
                    .InstructionIcon = enumInstructionIcon
                    .InstructionText = strInstructionText
                End With

                enumResult = obj.Show

            Else

                enumResult = _objUnitTestResponder.GetResponse
            End If

            OnLog(enumResult, strInstructionHeading, strInstructionText, intButtonsDisabledDelay, ex)
            Return enumResult
        End Function

#End Region

    End Class

End Namespace
